package com.example.rae.zookeepe.rorder.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class OrderController {

    // RestTemplate是有Springboot web组件提供 默认整合了ribbon负载均衡器
    // rest方式底层采用httpClient技术
    @Autowired
    private RestTemplate restTemplate;

    // DiscoveryClient 可以获取注册中心上的实例信息
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/discoveryClientMember")
    public List<ServiceInstance> discoveryClientMember(){
        List<ServiceInstance> instances = discoveryClient.getInstances("zk-member");
        for (ServiceInstance instance : instances) {
            System.out.println("url:"+instance.getUri());
        }
        return instances;
    }

    /**
     * 在springCloud中有两种调用第三方方式rest、fegin(springCloud)
     *
     * @return
     */
    @GetMapping("getOrder")
    public String getOrder() {
        // 有两种方式，一种采用别名调用，一种直接调用
//        String url = "http://192.168.0.104:8000/getMember";
        String url = "http://zk-member/getMember";
        String result = restTemplate.getForObject(url, String.class);
        System.out.println("订单服务调用会员服务，result:" + result);
        return result;
    }
}

